<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Page_lib {

	var $base_url			= ''; // 链接地址
	var $total_rows			=  0; // 总条数
	var $total_page			= 10; // 一页条数
	var $now_page			= 1; // 第几页
	var $rollPage			= 11;// 分页栏每页显示的页数

	var $get_page_tag		= "rows";
	var $suffix				= "";

	var $first_link			= '&lsaquo;'; //&lsaquo; << 首页
	var $next_link			= '&gt;';
	var $prev_link			= '&lt;';
	var $last_link			= '&rsaquo;'; // &rsaquo; >> 末页

	
	var $cur_tag_open		= '&nbsp;<strong>';
	var $cur_tag_close		= '</strong>';
	var $next_tag_open		= '&nbsp;';
	var $next_tag_close		= '&nbsp;';
	var $prev_tag_open		= '&nbsp;';
	var $prev_tag_close		= '';
	var $num_tag_open		= '&nbsp;';
	var $num_tag_close		= '';



	function links()
	{
		$output = "";

		//如果页数
		if($this->now_page <= 0 || $this->total_rows <= 0) return $output;

		/* 计算分页信息 */
		$totalPages = ceil($this->total_rows / $this->total_page); //总页数

		$now_cool_page      = $this->rollPage/2;
		$now_cool_page_ceil = ceil($now_cool_page);

		//上一页
		$up_row = $this->now_page - 1;
		$up_page = $up_row > 0 ? '<li><a href="' . $this->get_url($up_row) . '">' . $this->prev_link . '</a></li>' : '';

		//下一页
        $down_row  = $this->now_page + 1;
        $down_page = ($down_row <= $totalPages) ? '<li><a href="' . $this->get_url($down_row) . '">' . $this->next_link . '</a></li>' : '';

        //第一页
        $the_first = '';
        if($totalPages > $this->rollPage && ($this->now_page - $now_cool_page) >= 1)
        {
            $the_first = '<li><a href="' . $this->get_url(1) . '">' . $this->first_link . '</a></li>';
        }

        //最后一页
        $the_end = '';
        if($totalPages > $this->rollPage && ($this->now_page + $now_cool_page) < $this->totalPages)
        {
            $the_end = '<li><a href="' . $this->get_url($totalPages) . '">' . $this->last_link . '</a></li>';
        }

        //数字连接
        $link_page = "";
        for($i = 1; $i <= $totalPages; $i++){
        	if($i == $this->now_page)
        	{
        		$link_page .= '<li class="disabled"><a href="#">' . $i . '</a></li>';
        	}
        	else
        	{
        		$link_page .= '<li><a href="' . $this->get_url($i) . '">' . $i . '</a></li>';
        	}
        }

        $total_rows = "<li><span class=\"rows\">共 ".$this->total_rows." 条记录</span></li>";

        $output = "<div><ul class=\"pagination\">".
        	$the_first . $up_page . $link_page . $down_page .  $the_end . 
        	$total_rows .
        	"</ul></div>";
		
		return $output;
	}

	private function get_url($url = NULL)
	{
		$hz = http_build_query($this->suffix);
		//print_r($this->suffix);
		if($hz)
		{
			$hz .= "&" . $this->get_page_tag . "=" . $url;
		}
		else
		{
			$hz = "?" . $this->get_page_tag . "=" . $url;
		}

		$total_url = $this->base_url ."?". ltrim($hz,"?");
		return $total_url;
	}


	/**
	 * Constructor
	 *
	 * @access	public
	 * @param	array	initialization parameters
	 */
	public function __construct($params = array())
	{
		if (count($params) > 0)
		{
			$this->init($params);
		}

		log_message('debug', "Pagination Class Initialized");
	}

	// --------------------------------------------------------------------

	/**
	 * Initialize Preferences
	 *
	 * @access	public
	 * @param	array	initialization parameters
	 * @return	void
	 */
	function init($params = array())
	{
		if (count($params) > 0)
		{
			foreach ($params as $key => $val)
			{
				if (isset($this->$key))
				{
					$this->$key = $val;
				}
			}
		}
	}

	
}
// END Page_lib Class

/* End of file Page_lib.php */
/* Location: ./system/libraries/Page_lib.php */